package com.camemax.rabbitmq.utils;

import com.rabbitmq.client.*;

import java.util.Map;
public class PublisherModules implements Publishers {

    @Override
    public void publishMessage(String exchangeName, String routingKey, Map<String, Object> headersMap, String message) {
        ConnectionFactory factory = RabbitMQUtils.createFactory();
        Connection connection = null;
        Channel channel = null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();

            channel.basicPublish(
                    exchangeName,
                    routingKey,
                    new AMQP.BasicProperties.Builder().headers(headersMap).build(),
                    message.getBytes()
            );

            System.out.println("Publisher sends finished!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RabbitMQUtils.close(channel, connection);
        }
    }
}
